package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;

import com.sxfq.loansupermarket.backsystem.GunsApplication;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Deptinfos;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Role;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;

import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IGetListService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.DepartmentInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 *
 * @author wxx
 */
@Controller
@RequestMapping("/appWeb")
public class AppWebController extends BaseController {
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    IGetListService getListService;

    private String PREFIX = "/weixin/app/";

    private String PREFIY = "/weixin/";

    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    /**
     * 跳转到回访列表
     */
    @RequestMapping(value = "/returnVisit_list/{obj}")
    public String reservation_screenTable(@PathVariable String obj, Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return PREFIY + "/login.html";
        }
        if (ShiroKit.getUser().getTcList() == null) {
            String getListSql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
            List<TcInfo> tcList =sqlMapper.selectList(getListSql,TcInfo.class);
            ShiroKit.getUser().setTcList(tcList);
        }
        if (ShiroKit.getUser().getDoctorInfoList() == null) {
            String doctorListSql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + ShiroKit.getUser().getAbbreviation() + "' and (c.name='医生' or c.name='洁牙师') and a.status='1';";
            List<DoctorInfo> doctorInfoList = sqlMapper.selectList(doctorListSql, DoctorInfo.class);
            ShiroKit.getUser().setDoctorInfoList(doctorInfoList);
        }
        model.addAttribute("doctorList", ShiroKit.getUser().getDoctorInfoList());
        if (ShiroKit.getUser().getDepartmentList() == null) {
            String getListSql = "select a.department as department from " + ShiroKit.getUser().getPay_service_grade_info() + " a where a.is_upper=1 group by a.department order by a.id;";
            List<DepartmentInfo> departmentList=sqlMapper.selectList(getListSql,DepartmentInfo.class);
            ShiroKit.getUser().setDepartmentList(departmentList);
        }
        if (ShiroKit.getUser().getRolelist() == null) {
            String selectsql = "select * from role a where a.pid=(select id from role a where a.name='市场') group by a.num;";
            List<Role> rolelist = sqlMapper.selectList(selectsql, Role.class);
            ShiroKit.getUser().setRolelist(rolelist);
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        if (ShiroKit.getSessionAttr("outpatientManagementList") == null) {
            List<Map<String, Object>> outpatientManagementList = getListService.outpatientManagementList();
            ShiroKit.setSessionAttr("outpatientManagementList", outpatientManagementList);
        }
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("rolelist", ShiroKit.getUser().getRolelist());
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("departmentList",ShiroKit.getUser().getDepartmentList());
        model.addAttribute("tcList", ShiroKit.getUser().getTcList());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("obj", obj);
        return PREFIX + obj+"/patient/" + obj + "_returnVisit_list.html";
    }

}